package com.aptemo.webetek.server.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.aptemo.webetek.client.enums.EnumEventStatus;
import com.aptemo.webetek.shared.EventBean;
import com.aptemo.webetek.shared.PickBean;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Bet
{
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key				id;

	// @Persistent
	// private UserGroup team;
	@SuppressWarnings("unused")
	@Persistent
	@Extension(vendorName = "datanucleus", key = "gae.parent-pk", value = "true")
	private Key				parentKey;

	private String			sid;

	// @Persistent(mappedBy = "bet")
	@Persistent
	// @Element(dependent = "true")
	private List<Pick>		picks		= new ArrayList<Pick>();

	@Persistent
	private List<Key>		labels		= new ArrayList<Key>();

	@Persistent
	private String			title;

	@Persistent
	private String			description;

	// @Persistent
	// private EnumEventType type = EnumEventType.N_WAY;

	@Persistent
	private Date			date;

	@Persistent
	private EnumEventStatus	status;

	// used for mark delete
	@Persistent
	private Date			activeUntil	= null;

	@Persistent
	private String			resultDescription;

	public Bet(Date date, String atitle, String adescription)
	{
		title = atitle;
		description = adescription;
		// setType(atype);
		setDate(date);
		setStatus(EnumEventStatus.ACTIVE);
	}

	public void setKey(Key key)
	{
		this.id = key;
	}

	public Key getKey()
	{
		return this.id;
	}

	public String getTitle()
	{
		return title;
	}

	public void setTitle(String atitle)
	{
		title = atitle;
	}

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String adesc)
	{
		description = adesc;
	}

	// public void setType(EnumEventType type)
	// {
	// this.type = type;
	// }
	//
	// public EnumEventType getType()
	// {
	// return type;
	// }

	public void setDate(Date date)
	{
		this.date = date;
	}

	public Date getDate()
	{
		return date;
	}

	public void setPicks(List<Pick> picks)
	{
		this.picks = picks;
	}

	public List<Pick> getPicks()
	{
		return picks;
	}

	public void setSid(String sid)
	{
		this.sid = sid;
	}

	public String getSid()
	{
		if (sid == null)
		{
			this.sid = KeyFactory.keyToString(id);
		}
		return sid;
	}

	public int getPickedSum()
	{
		int sum = 0;
		for (Pick item : picks)
		{
			sum += item.getPicked();
		}
		return sum;
	}

	public double getWeightTotal()
	{
		int sum = 0;
		for (Pick item : picks)
		{
			sum += item.getWeight();
		}
		return sum;
	}

	public EventBean toBean()
	{
		EventBean bet = new EventBean(date, title, description);
		bet.setKey(getSid());
		bet.setStatus(status);
		bet.setResultDescription(resultDescription);
		// bet.setBetType(type);
		bet.setTeamId(KeyFactory.keyToString(parentKey));

		// not implemented
		if (labels != null)
		{
			ArrayList<String> lbeans = new ArrayList<String>();
			for (Key key : labels)
			{
				String s = KeyFactory.keyToString(key);
				lbeans.add(s);
			}
			bet.setLabels(lbeans);
		}

		// TODO performace overkill
		ArrayList<PickBean> pickbeans = new ArrayList<PickBean>();
		for (Pick item : picks)
		{
			PickBean pbean = item.toBean(bet);
			pickbeans.add(pbean);
		}
		bet.setPicks(pickbeans);

		return bet;
	}

	public void setStatus(EnumEventStatus status)
	{
		this.status = status;
	}

	public EnumEventStatus getStatus()
	{
		return status;
	}

	public void addPick(Pick pick)
	{
		picks.add(pick);
	}

	public void clearPicks()
	{
		picks.clear();
	}

	public void setLabels(List<Key> labels)
	{
		this.labels = labels;
	}

	public List<Key> getLabels()
	{
		return labels;
	}

	// public UserGroup getParent()
	// {
	// return parent;
	// }

	public void addLabel(Key key)
	{
		labels.add(key);
	}

	public void setActiveUntil(Date activeUntil)
	{
		this.activeUntil = activeUntil;
	}

	public Date getActiveUntil()
	{
		return activeUntil;
	}

	public String getResultDescription()
	{
		return resultDescription;
	}

	public void setResultDescription(String resultDescription)
	{
		this.resultDescription = resultDescription;
	}

	public boolean containsLabel(Key key)
	{
		for (Key labelkey : labels)
		{
			if (labelkey.equals(key))
			{
				return true;
			}
		}
		return false;
	}

	// public UserGroup getParent()
	// {
	// return team; // pm.getObjectById(UserGroup.class, id.getParent());
	// }
}
